package SE010;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class changeCompleteStatus
 */
@WebServlet("/changeCompleteStatus")
public class changeCompleteStatus extends HttpServlet{//修改学生的任务完成情况
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String UserID = request.getParameter("UserID");
        String InviteID = request.getParameter("InviteID");
        String taskID = request.getParameter("taskID");
        String num = request.getParameter("num");
        String taskContent = request.getParameter("taskContent").replaceAll("@N", "").replaceAll("@Y", "");
        String[] taskContents = taskContent.split(";");
        String taskCompleteStatu = null;//记录获取的题目完成状态
        String taskFormName = null;//记录变化的表单控件名称
        StringBuilder taskBuilder = new StringBuilder().append("");
        float completeYes = 0;//统计已完成的题目书数量
        float total = taskContents.length;//所有的题目数量
        for (int i = 0; i < taskContents.length; i++) {
            taskFormName = "task"+i;
            taskCompleteStatu = request.getParameter(taskFormName);
            if(taskCompleteStatu != null && taskCompleteStatu.equals("Y")) {
                taskBuilder.append(taskContents[i]+"@Y;");
                completeYes++;
            }
            if(taskCompleteStatu == null) {
                taskBuilder.append(taskContents[i]+"@N;");
            }   
        }
        float myNewCompleteRate = completeYes/total;
        taskContent = taskBuilder.toString();//重新修改完完成状态后的任务内容
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "root";
        String sql = "update taskcontent_stu set TaskContent='"+taskContent+"',MyCompleteRate="+myNewCompleteRate+" where StudentID='"+UserID+"' and TaskID='"+taskID+"'";
        Connection conn = null;//用于建立与数据库的连接
        Statement sta = null;
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作 
            conn = DriverManager.getConnection(url, userName, password);
            if(conn != null) {
                sta = conn.createStatement();
            }
            if(sta != null) {
                sta.execute(sql);
            }
        }catch(Exception e){
            System.out.println(e);
        }finally {
            try {
                if(sta != null)
                    sta.close();
                if(conn != null)
                    conn.close();
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        out.print("<html>");
        out.print("<head>");
        out.print("<title>提交成功</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<center>");
        out.print("提交成功!"+"<br>");
        out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showTaskContent?UserID="+URLEncoder.encode(UserID)+"&InviteID="+URLEncoder.encode(InviteID)+"&taskID="+URLEncoder.encode(taskID)+"&num="+URLEncoder.encode(num)+"'\">");
        //out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showTaskContent?UserID="+UserID+"&InviteID="+InviteID+"&taskID="+taskID+"&num="+num+"'\">");
        out.print("</center>");
        out.print("</form>");
        out.print("</body>");
        out.print("</html>");
    }
}
